package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.platform.dao.IFpFundBuynoticeDao;
import com.financing.platform.dao.IFpFundInfoDao;
import com.financing.platform.dao.IProductOrderDataDao;
import com.financing.platform.dto.FpFundBuynoticeDTO;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.ProductOrderDataDTO;
import com.financing.platform.service.IFpFundInfoService;

/**
 * @Description:Service实现类
 * @version 1.0
 * @created caojiayao
 */
 
@Service(value="fpFundInfoService")
public class FpFundInfoServiceImpl implements IFpFundInfoService {
    @Autowired
	private IFpFundInfoDao fpFundInfoDao;
	
    @Autowired
	private IFpFundBuynoticeDao fpFundBuynoticeDao;
    
    @Autowired
    private IProductOrderDataDao productOrderDataDao;
    
    
    private Logger logger=Logger.getLogger(FpFundInfoServiceImpl.class);
	
	@Override
	public FpFundInfoDTO getFpFundInfo(FpFundInfoDTO dto) {
		dto = fpFundInfoDao.getFpFundInfo(dto);
		if(dto == null){
			return null;
		}
		FpFundBuynoticeDTO  bean = new FpFundBuynoticeDTO();
		bean.setJjbm(dto.getJjbm());
		bean = fpFundBuynoticeDao.getFpFundBuynotice(bean);
		dto.setFpFundBuynoticeDTO(bean);
		
		ProductOrderDataDTO productOrderDataDto=new ProductOrderDataDTO();
		productOrderDataDto.setpId(dto.getId());
		productOrderDataDto.setpType("05");
		try {
			String tjzls=productOrderDataDao.queryProductOrderData(productOrderDataDto);
			if(StringUtils.isNotBlank(tjzls)) dto.setTjzls(tjzls.replace("0","").replace("1,2,3,4", "1"));
		} catch (SQLException e) {
			logger.error("queryProductOrderData error",e);
		}
		
		return dto;
	}
		
	@Transactional(rollbackFor=Exception.class)
	@Override
	public Integer insertFpFundInfo(FpFundInfoDTO dto,FpFundBuynoticeDTO buyNoticeDto) throws SQLException {
		//插入扩展表 -购买须知
		fpFundBuynoticeDao.insertFpFundBuynotice(buyNoticeDto);
		
		int fundId= fpFundInfoDao.insertFpFundInfo(dto);
		//图片类型(01:身份证正面；02：身份证反面；03：银行卡；04：打款凭条；05：签字页；06：合同封面；07手持签字页和合同封面；08：其他)
		ProductOrderDataDTO productOrderDataDto=new ProductOrderDataDTO();
		productOrderDataDto.setpId(fundId);
		productOrderDataDto.setpType("05");
		//插入 产品需要的 附件
		if(dto.getTjzls()!=null){
			productOrderDataDto.setCreateBy(dto.getCreateBy());
			productOrderDataDto.setCreateDate(dto.getCreateDate());
			productOrderDataDto.setUpdateBy(dto.getUpdateBy());
			productOrderDataDto.setUpdateDate(dto.getUpdateDate());
			
			insertOrderData(dto.getTjzls(),productOrderDataDto);
		}else{
			logger.info("product not have att file");
		}
		
		return fundId;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public Integer updateFpFundInfo(Map map) throws SQLException {
		fpFundBuynoticeDao.updateFpFundBuynotice(map);	
		String tjzls=map.get("tjzls")==null?"":(String.valueOf(map.get("tjzls")));
		if(!StringUtils.isBlank(tjzls)){
			//图片类型(01:身份证正面；02：身份证反面；03：银行卡；04：打款凭条；05：签字页；06：合同封面；07手持签字页和合同封面；08：其他)
			ProductOrderDataDTO productOrderDataDto=new ProductOrderDataDTO();
			productOrderDataDto.setpType("05");
			productOrderDataDto.setpId(map.get("id")==null?0:Integer.parseInt(map.get("id")+""));
			productOrderDataDto.setCreateBy(map.get("updateBy")==null?"":(map.get("updateBy")+""));
			productOrderDataDto.setUpdateBy(map.get("updateBy")==null?"":(map.get("updateBy")+""));
			
			//删除所有该产品附件要求
			productOrderDataDao.delProductOrderData(productOrderDataDto);

			insertOrderData(tjzls, productOrderDataDto);
		}
		
		
		return fpFundInfoDao.updateFpFundInfo(map);
	}
	//插入 产品需要的 附件
	public void insertOrderData(String tjzls,
			ProductOrderDataDTO productOrderDataDto) throws SQLException {
		if(StringUtils.isNotBlank(tjzls)){
			for (int x = 0; x < tjzls.split(",").length; x++) {
				if(StringUtils.isBlank(tjzls.split(",")[x])){
					continue;
				}
				if("1".equals(tjzls.split(",")[x])){
					for (int i = 1;i<=4; i++) {
						productOrderDataDto.setoPhoType("0"+i);
						productOrderDataDao.addProductOrderData(productOrderDataDto);
					}
				}else{
					productOrderDataDto.setoPhoType("0"+tjzls.split(",")[x]);
					productOrderDataDao.addProductOrderData(productOrderDataDto);
				}
			}
		}
	}
	
	@Override
	public Integer delFpFundInfo(Integer id) {
		return fpFundInfoDao.delFpFundInfo(id);
	}
	
	@Override
	public List<FpFundInfoDTO> listFpFundInfo(FpFundInfoDTO dto) {
		return fpFundInfoDao.listFpFundInfo(dto);
	}
	
	@Override
	public Integer countFpFundInfo(FpFundInfoDTO dto){
		return fpFundInfoDao.countFpFundInfo(dto);
	}
	
	public List<FpFundInfoDTO> listFpFundInfoByCompanyId(String CompanyId){
		return fpFundInfoDao.listFpFundInfoByCompanyId(CompanyId);
	}
	
	public List<FpFundInfoDTO> listFpFundInfoByManagerId(String ManagerId){
		return fpFundInfoDao.listFpFundInfoByManagerId(ManagerId);
	}

	@Override
	public Map<String, Object>  queryFundInfoNameById(String id) {
		return fpFundInfoDao.queryFundInfoNameById(id);
	}
}
